<template>
  <view>
    <web-view :src="allUrl" :update-title='false'></web-view>
  </view>
</template>

<script>
export default {
  name: "webview",
  data() {
    return {
      viewerUrl: '/hybrid/html/web/viewer.html',
      allUrl: '',
      pdfUrl: "",
      title: '',
    }
  },
  onLoad(options) { //需要传入pdf文件的url
    let {pdfUrl, title, show} = options
    uni.setNavigationBarTitle({title})
    this.pdfUrl = decodeURIComponent(pdfUrl)
    this.title = title
    /* #ifdef APP-PLUS */
    this.openInApp(options)
    /* #endif */
    /* #ifdef H5 */
    this.openInH5()
    /* #endif */
  },
  mounted() {
  },
  methods: {
    openInApp(options) {
      // this.pdfUrl = "https://www.gjtool.cn/pdfh5/git.pdf" // todo 测试pdf地址
      /* #ifdef APP-PLUS */
      uni.downloadFile({
        url: this.pdfUrl,
        success: (res) => {
          if (res.statusCode === 200) {
            let tempFilePath = res.tempFilePath
            let localFilePath = plus.io.convertLocalFileSystemURL(tempFilePath)
            this.allUrl = this.viewerUrl + '?file=' + localFilePath
          }
        },
        fail(err) {
          console.error(err)
        }
      });
      /* #endif */
      /* #ifdef H5 */
      this.allUrl = this.pdfUrl
      /* #endif */
    },
    openInH5() {
      this.allUrl = this.viewerUrl + '?file=' + this.pdfUrl
    }
  }
}
</script>

<style scoped>

</style>
